package com.zis.auction.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.auction.dao.AuctRegionDao;
import com.zis.auction.model.AuctRegion;
import com.zis.auction.service.IAuctRegionInter;
import com.zis.base.impl.BaseInterImpl;
import com.zis.core.Constant;
import com.zis.core.ResponseEntity;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2017/12/26
 */
@Service
@Transactional("txManager")
public class AuctRegionInterImpl extends BaseInterImpl<AuctRegion> implements IAuctRegionInter {

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private AuctRegionDao auctRegionDao;

    @Override
    public String insert(AuctRegion auctRegion) throws Exception {
        int num = sqlManager.insert(auctRegion);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String update(AuctRegion auctRegion) throws Exception {
        int num = sqlManager.updateTemplateById(auctRegion);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String delete(String id) throws Exception {
        int num = sqlManager.deleteById(AuctRegion.class, id);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public AuctRegion selectById(String id) throws Exception {
        return sqlManager.unique(AuctRegion.class, id);
    }

    @Override
    public List<AuctRegion> findByCondition(AuctRegion auctRegion) throws Exception {
        return sqlManager.template(auctRegion);
    }

    /**
     * 按照级别查询转为list
     * @return
     */
    @Override
    public List<JSONObject> findToList() throws Exception {
        List<JSONObject> jsonList = new ArrayList<>();
        List<AuctRegion> rootList = auctRegionDao.findByParentId("0");
        for (AuctRegion region : rootList) {
            JSONObject rootJson = new JSONObject();
            rootJson.put("value", region.getRegionCode());
            rootJson.put("label", region.getRegionName());
            // 获取二级地点
            List<AuctRegion> subList = auctRegionDao.findByParentId(region.getRegionCode());
            JSONArray subArray = new JSONArray();
            for (AuctRegion subRegion : subList) {
                JSONObject subJson = new JSONObject();
                subJson.put("value", subRegion.getRegionCode());
                subJson.put("label", subRegion.getRegionName());
                // 获取三级地点
                List<AuctRegion> sub3List = auctRegionDao.findByParentId(subRegion.getRegionCode());
                JSONArray sub3Array = new JSONArray();
                for (AuctRegion sub3Region : sub3List) {
                    JSONObject sub3Json = new JSONObject();
                    sub3Json.put("value", sub3Region.getRegionCode());
                    sub3Json.put("label", sub3Region.getRegionName());
                    sub3Array.add(sub3Json);
                }
                subJson.put("children", sub3Array);
                subArray.add(subJson);
            }
            rootJson.put("children", subArray);
            jsonList.add(rootJson);
        }
        return jsonList;
    }
}
